Mutates the original array to filter out the values specified.
Use Array.prototype.filter() and Array.prototype.includes() to pull out the values that are not needed.
Use Array.prototype.length = 0 to mutate the passed in an array by resetting it's length to zero and Array.prototype.push() to re-populate it with only the pulled values.
去除指定的陣列
const pull = (arr, ...args) => {
let argState = Array.isArray(args[0]) ? args[0] : args;
let pulled = arr.filter(v => !argState.includes(v));
arr.length = 0;
pulled.forEach(v => arr.push(v));
};
//EXAMPLES
let myArray = ['a', 'b', 'c', 'a', 'b', 'c'];
pull(myArray, 'a', 'c'); // myArray = [ 'b', 'b' ]
陣列 (array) 的 filter() 方法用來根據你指定的測試函數,從一個陣列中篩選出符合條件的元素。
function isBigEnough(element) {
return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
console.log(filtered);//[ 12, 130, 44 ]
isArray() 方法用於判斷是否為陣列,如果對像是陣列返回true,否則返回false。
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = Array.isArray(fruits);
}